<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>InFact: src/infact/stream-tokenizer.h Source File</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />

<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
  $(document).ready(function() { searchBox.OnSelectItem(0); });
</script>

</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">InFact
   
   </div>
   <div id="projectbrief">Interpreter and factory for easily creating C++ objects at run-time</div>
  </td>
  
  
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.6.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
  <div id="navrow1" class="tabs">
    <ul class="tablist">
      <li><a href="index.html"><span>Main&#160;Page</span></a></li>
      <li><a href="namespaces.html"><span>Namespaces</span></a></li>
      <li><a href="annotated.html"><span>Classes</span></a></li>
      <li class="current"><a href="files.html"><span>Files</span></a></li>
      <li>
        <div id="MSearchBox" class="MSearchBoxInactive">
        <span class="left">
          <img id="MSearchSelect" src="search/mag_sel.png"
               onmouseover="return searchBox.OnSearchSelectShow()"
               onmouseout="return searchBox.OnSearchSelectHide()"
               alt=""/>
          <input type="text" id="MSearchField" value="Search" accesskey="S"
               onfocus="searchBox.OnSearchFieldFocus(true)" 
               onblur="searchBox.OnSearchFieldFocus(false)" 
               onkeyup="searchBox.OnSearchFieldChange(event)"/>
          </span><span class="right">
            <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
          </span>
        </div>
      </li>
    </ul>
  </div>
  <div id="navrow2" class="tabs2">
    <ul class="tablist">
      <li><a href="files.html"><span>File&#160;List</span></a></li>
      <li><a href="globals.html"><span>File&#160;Members</span></a></li>
    </ul>
  </div>
</div>
<div class="header">
  <div class="headertitle">
<div class="title">src/infact/stream-tokenizer.h</div>  </div>
</div><!--header-->
<div class="contents">
<a href="stream-tokenizer_8h.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright 2014, Google Inc.</span>
<a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
<a name="l00003"></a>00003 <span class="comment">//</span>
<a name="l00004"></a>00004 <span class="comment">// Redistribution and use in source and binary forms, with or without</span>
<a name="l00005"></a>00005 <span class="comment">// modification, are permitted provided that the following conditions are</span>
<a name="l00006"></a>00006 <span class="comment">// met:</span>
<a name="l00007"></a>00007 <span class="comment">//</span>
<a name="l00008"></a>00008 <span class="comment">//   * Redistributions of source code must retain the above copyright</span>
<a name="l00009"></a>00009 <span class="comment">//     notice, this list of conditions and the following disclaimer.</span>
<a name="l00010"></a>00010 <span class="comment">//   * Redistributions in binary form must reproduce the above</span>
<a name="l00011"></a>00011 <span class="comment">//     copyright notice, this list of conditions and the following disclaimer</span>
<a name="l00012"></a>00012 <span class="comment">//     in the documentation and/or other materials provided with the</span>
<a name="l00013"></a>00013 <span class="comment">//     distribution.</span>
<a name="l00014"></a>00014 <span class="comment">//   * Neither the name of Google Inc. nor the names of its</span>
<a name="l00015"></a>00015 <span class="comment">//     contributors may be used to endorse or promote products derived from</span>
<a name="l00016"></a>00016 <span class="comment">//     this software without specific prior written permission.</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018 <span class="comment">// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS</span>
<a name="l00019"></a>00019 <span class="comment">// &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT</span>
<a name="l00020"></a>00020 <span class="comment">// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR</span>
<a name="l00021"></a>00021 <span class="comment">// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT</span>
<a name="l00022"></a>00022 <span class="comment">// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,</span>
<a name="l00023"></a>00023 <span class="comment">// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT</span>
<a name="l00024"></a>00024 <span class="comment">// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,</span>
<a name="l00025"></a>00025 <span class="comment">// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY</span>
<a name="l00026"></a>00026 <span class="comment">// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT</span>
<a name="l00027"></a>00027 <span class="comment">// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE</span>
<a name="l00028"></a>00028 <span class="comment">// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.</span>
<a name="l00029"></a>00029 <span class="comment">// -----------------------------------------------------------------------------</span>
<a name="l00030"></a>00030 <span class="comment">//</span>
<a name="l00031"></a>00031 <span class="comment">//</span>
<a name="l00035"></a>00035 <span class="comment"></span>
<a name="l00036"></a>00036 <span class="preprocessor">#ifndef INFACT_STREAM_TOKENIZER_H_</span>
<a name="l00037"></a>00037 <span class="preprocessor"></span><span class="preprocessor">#define INFACT_STREAM_TOKENIZER_H_</span>
<a name="l00038"></a>00038 <span class="preprocessor"></span>
<a name="l00039"></a>00039 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00040"></a>00040 <span class="preprocessor">#include &lt;set&gt;</span>
<a name="l00041"></a>00041 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00042"></a>00042 <span class="preprocessor">#include &lt;stdexcept&gt;</span>
<a name="l00043"></a>00043 <span class="preprocessor">#include &lt;string&gt;</span>
<a name="l00044"></a>00044 <span class="preprocessor">#include &lt;string.h&gt;</span>
<a name="l00045"></a>00045 <span class="preprocessor">#include &lt;vector&gt;</span>
<a name="l00046"></a>00046 
<a name="l00047"></a>00047 <span class="preprocessor">#include &quot;<a class="code" href="error_8h.html" title="Provides an error handling function that optionally throws an exception.">error.h</a>&quot;</span>
<a name="l00048"></a>00048 
<a name="l00049"></a>00049 <span class="keyword">namespace </span>infact {
<a name="l00050"></a>00050 
<a name="l00051"></a>00051 <span class="keyword">using</span> std::istream;
<a name="l00052"></a>00052 <span class="keyword">using</span> std::istringstream;
<a name="l00053"></a>00053 <span class="keyword">using</span> std::ostringstream;
<a name="l00054"></a>00054 <span class="keyword">using</span> std::set;
<a name="l00055"></a>00055 <span class="keyword">using</span> std::streampos;
<a name="l00056"></a>00056 <span class="keyword">using</span> std::string;
<a name="l00057"></a>00057 <span class="keyword">using</span> std::vector;
<a name="l00058"></a>00058 <span class="keyword">using</span> std::cerr;
<a name="l00059"></a>00059 <span class="keyword">using</span> std::endl;
<a name="l00060"></a>00060 
<a name="l00065"></a>00065 <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *default_reserved_words[] = {
<a name="l00066"></a>00066   <span class="stringliteral">&quot;-&quot;</span>,
<a name="l00067"></a>00067   <span class="stringliteral">&quot;nullptr&quot;</span>,
<a name="l00068"></a>00068   <span class="stringliteral">&quot;NULL&quot;</span>,
<a name="l00069"></a>00069   <span class="stringliteral">&quot;false&quot;</span>,
<a name="l00070"></a>00070   <span class="stringliteral">&quot;true&quot;</span>,
<a name="l00071"></a>00071   <span class="stringliteral">&quot;bool&quot;</span>,
<a name="l00072"></a>00072   <span class="stringliteral">&quot;int&quot;</span>,
<a name="l00073"></a>00073   <span class="stringliteral">&quot;double&quot;</span>,
<a name="l00074"></a>00074   <span class="stringliteral">&quot;string&quot;</span>,
<a name="l00075"></a>00075   <span class="stringliteral">&quot;bool[]&quot;</span>,
<a name="l00076"></a>00076   <span class="stringliteral">&quot;int[]&quot;</span>,
<a name="l00077"></a>00077   <span class="stringliteral">&quot;double[]&quot;</span>,
<a name="l00078"></a>00078   <span class="stringliteral">&quot;string[]&quot;</span>,
<a name="l00079"></a>00079 };
<a name="l00080"></a>00080 
<a name="l00082"></a><a class="code" href="stream-tokenizer_8h.html#aa6dcca782316d8f09a268865ae5f924b">00082</a> <span class="preprocessor">#define DEFAULT_RESERVED_CHARS &quot;(){},=;/&quot;</span>
<a name="l00083"></a>00083 <span class="preprocessor"></span>
<a name="l00089"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html">00089</a> <span class="keyword">class </span><a class="code" href="classinfact_1_1_stream_tokenizer.html" title="A simple class for tokenizing a stream of tokens for the formally specified language used to construc...">StreamTokenizer</a> {
<a name="l00090"></a>00090  <span class="keyword">public</span>:
<a name="l00094"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3">00094</a>   <span class="keyword">enum</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3" title="The set of types of tokens read by this stream tokenizer.">TokenType</a> {
<a name="l00095"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3ab746a48f2f16545e93a343b15ce7f137">00095</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3ab746a48f2f16545e93a343b15ce7f137">EOF_TYPE</a>,
<a name="l00096"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a68d8e01c0b3e418c30e2fed7d565d71a">00096</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a68d8e01c0b3e418c30e2fed7d565d71a">RESERVED_CHAR</a>,
<a name="l00097"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a1c9eaa574c618f243aa51efe253d3118">00097</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a1c9eaa574c618f243aa51efe253d3118">RESERVED_WORD</a>,
<a name="l00098"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3adf5d994582c00a0d2bca91e6d3505409">00098</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3adf5d994582c00a0d2bca91e6d3505409">STRING</a>,
<a name="l00099"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3adbb02ac8845cd4a289387df4bf554aea">00099</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3adbb02ac8845cd4a289387df4bf554aea">NUMBER</a>,
<a name="l00100"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a61f308d29c6dd30996fcb120b6344e41">00100</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3a61f308d29c6dd30996fcb120b6344e41">IDENTIFIER</a>
<a name="l00101"></a>00101   };
<a name="l00102"></a>00102 
<a name="l00104"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#acd4baaa4ccb442e4398550e2f8c76419">00104</a>   <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *<a class="code" href="classinfact_1_1_stream_tokenizer.html#acd4baaa4ccb442e4398550e2f8c76419" title="Returns a string type name for the specified TokenType constant.">TypeName</a>(<a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3" title="The set of types of tokens read by this stream tokenizer.">TokenType</a> token_type) {
<a name="l00105"></a>00105     <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">char</span> *names[] = {
<a name="l00106"></a>00106       <span class="stringliteral">&quot;EOF&quot;</span>, <span class="stringliteral">&quot;RESERVED_CHAR&quot;</span>, <span class="stringliteral">&quot;RESERVED_WORD&quot;</span>, <span class="stringliteral">&quot;STRING&quot;</span>, <span class="stringliteral">&quot;NUMBER&quot;</span>, <span class="stringliteral">&quot;IDENTIFIER&quot;</span>
<a name="l00107"></a>00107     };
<a name="l00108"></a>00108     <span class="keywordflow">return</span> names[token_type];
<a name="l00109"></a>00109   }
<a name="l00110"></a>00110 
<a name="l00112"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html">00112</a>   <span class="keyword">struct </span><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html" title="Information about a token read from the underlying stream.">Token</a> {
<a name="l00114"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#acab9e018c4ea60a98e313e3a02771fb5">00114</a>     <span class="keywordtype">string</span> <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#acab9e018c4ea60a98e313e3a02771fb5" title="The token itself.">tok</a>;
<a name="l00116"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a8fdb3551211d91cdb94be0110313f9a8">00116</a>     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3" title="The set of types of tokens read by this stream tokenizer.">TokenType</a> <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a8fdb3551211d91cdb94be0110313f9a8" title="The token&rsquo;s type.">type</a>;
<a name="l00117"></a>00117 
<a name="l00118"></a>00118     <span class="comment">// The following three fields capture information about the underlying</span>
<a name="l00119"></a>00119     <span class="comment">// byte stream at the time this token was read from it.</span>
<a name="l00120"></a>00120 
<a name="l00122"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#ade06808bb0ebd847b4d5d7c7b07a0092">00122</a>     <span class="keywordtype">size_t</span> <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#ade06808bb0ebd847b4d5d7c7b07a0092" title="The starting byte of the token in the underlying stream.">start</a>;
<a name="l00124"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a08b8a9e4a700aef306fcb1a32f5bdfac">00124</a>     <span class="keywordtype">size_t</span> <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a08b8a9e4a700aef306fcb1a32f5bdfac" title="The line number of the first byte of the token in the underlying stream.">line_number</a>;
<a name="l00127"></a><a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a39463d4c73522c5a8724e2c72984ce44">00127</a>     <span class="keywordtype">size_t</span> <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html#a39463d4c73522c5a8724e2c72984ce44" title="The current position in the underlying stream just after reading this token.">curr_pos</a>;
<a name="l00128"></a>00128   };
<a name="l00129"></a>00129 
<a name="l00136"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a05d4bb3bf40ae8b87e2438a73491432b">00136</a>   <a class="code" href="classinfact_1_1_stream_tokenizer.html#a05d4bb3bf40ae8b87e2438a73491432b" title="Constructs a new instance around the specified byte stream.">StreamTokenizer</a>(istream &amp;is,
<a name="l00137"></a>00137                   <span class="keyword">const</span> <span class="keywordtype">char</span> *reserved_chars = <a class="code" href="stream-tokenizer_8h.html#aa6dcca782316d8f09a268865ae5f924b" title="Default set of reserved characters for the StreamTokenizer class.">DEFAULT_RESERVED_CHARS</a>) :
<a name="l00138"></a>00138       is_(is), num_read_(0), line_number_(0), eof_reached_(false),
<a name="l00139"></a>00139       next_token_idx_(0) {
<a name="l00140"></a>00140     Init(reserved_chars);
<a name="l00141"></a>00141   }
<a name="l00142"></a>00142 
<a name="l00149"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a9beb17df057c000bb3f40ffa9922553f">00149</a>   <a class="code" href="classinfact_1_1_stream_tokenizer.html#a05d4bb3bf40ae8b87e2438a73491432b" title="Constructs a new instance around the specified byte stream.">StreamTokenizer</a>(<span class="keyword">const</span> <span class="keywordtype">string</span> &amp;s,
<a name="l00150"></a>00150                   <span class="keyword">const</span> <span class="keywordtype">char</span> *reserved_chars = <a class="code" href="stream-tokenizer_8h.html#aa6dcca782316d8f09a268865ae5f924b" title="Default set of reserved characters for the StreamTokenizer class.">DEFAULT_RESERVED_CHARS</a>) :
<a name="l00151"></a>00151       sstream_(s), is_(sstream_), num_read_(0), line_number_(0),
<a name="l00152"></a>00152       eof_reached_(false), next_token_idx_(0) {
<a name="l00153"></a>00153     Init(reserved_chars);
<a name="l00154"></a>00154   }
<a name="l00155"></a>00155 
<a name="l00158"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a7c1acdd8c33cfc468ae46672f7746c21">00158</a>   <span class="keywordtype">void</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a7c1acdd8c33cfc468ae46672f7746c21" title="Sets the set of &ldquo;reserved words&rdquo; used by this stream tokenizer.">set_reserved_words</a>(set&lt;string&gt; &amp;reserved_words) {
<a name="l00159"></a>00159     reserved_words_ = reserved_words;
<a name="l00160"></a>00160   }
<a name="l00161"></a>00161 
<a name="l00163"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#aec719b0937bf3b1ee95ef7339cb398f5">00163</a>   <span class="keyword">virtual</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#aec719b0937bf3b1ee95ef7339cb398f5" title="Destroys this instance.">~StreamTokenizer</a>() {
<a name="l00164"></a>00164     <span class="keyword">delete</span>[] reserved_chars_;
<a name="l00165"></a>00165   }
<a name="l00166"></a>00166 
<a name="l00169"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a3790d1692c84c79af738a9ade303cbd7">00169</a>   <span class="keywordtype">string</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a3790d1692c84c79af738a9ade303cbd7" title="Returns the entire sequence of characters read so far by this stream tokenizer as a newly constructed...">str</a>() { <span class="keywordflow">return</span> oss_.str(); }
<a name="l00170"></a>00170 
<a name="l00174"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a2f2705cec4fa2f38202e8c3022482fd2">00174</a>   <span class="keywordtype">size_t</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2f2705cec4fa2f38202e8c3022482fd2" title="Returns the number of bytes read from the underlying byte stream just after scanning the most recent ...">tellg</a>()<span class="keyword"> const </span>{
<a name="l00175"></a>00175     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">HasPrev</a>() ? token_[next_token_idx_ - 1].curr_pos : 0;
<a name="l00176"></a>00176   }
<a name="l00177"></a>00177 
<a name="l00181"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#afaed5415a9cd640bfda043014bed5fa6">00181</a>   <span class="keywordtype">size_t</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#afaed5415a9cd640bfda043014bed5fa6" title="Returns the number of lines read from the underlying byte stream, where a line is any number of bytes...">line_number</a>()<span class="keyword"> const </span>{
<a name="l00182"></a>00182     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>() ? token_[next_token_idx_].line_number : line_number_;
<a name="l00183"></a>00183   }
<a name="l00184"></a>00184 
<a name="l00186"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6">00186</a>   <span class="keywordtype">bool</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> next_token_idx_ &lt; token_.size(); }
<a name="l00187"></a>00187 
<a name="l00188"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">00188</a>   <span class="keywordtype">bool</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">HasPrev</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> next_token_idx_ &gt; 0; }
<a name="l00189"></a>00189 
<a name="l00190"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac72a1a4fd331f44b850d2600a68a5063">00190</a>   <span class="keywordtype">string</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac72a1a4fd331f44b850d2600a68a5063">PeekPrev</a>()<span class="keyword"> const </span>{
<a name="l00191"></a>00191     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">HasPrev</a>() ? token_[next_token_idx_ - 1].tok : <span class="stringliteral">&quot;&quot;</span>;
<a name="l00192"></a>00192   }
<a name="l00193"></a>00193 
<a name="l00194"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ad7c5c716ebfadf303e8d4371e6bb229b">00194</a>   <span class="keywordtype">size_t</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ad7c5c716ebfadf303e8d4371e6bb229b">PeekPrevTokenStart</a>()<span class="keyword"> const </span>{
<a name="l00195"></a>00195     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">HasPrev</a>() ? token_[next_token_idx_ - 1].start : 0;
<a name="l00196"></a>00196   }
<a name="l00197"></a>00197 
<a name="l00198"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ad4fe21722c3e34d4339f12691146b7a5">00198</a>   <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3" title="The set of types of tokens read by this stream tokenizer.">TokenType</a> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ad4fe21722c3e34d4339f12691146b7a5">PeekPrevTokenType</a>()<span class="keyword"> const </span>{
<a name="l00199"></a>00199     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a2d8c5ba3138d99c7a13fa49091885d0e">HasPrev</a>() ? token_[next_token_idx_ - 1].type : <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3ab746a48f2f16545e93a343b15ce7f137">EOF_TYPE</a>;
<a name="l00200"></a>00200   }
<a name="l00201"></a>00201 
<a name="l00203"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6827f00a3da760e23241e890becee0c">00203</a>   <span class="keywordtype">string</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6827f00a3da760e23241e890becee0c" title="Returns the next token in the token stream.">Next</a>() {
<a name="l00204"></a>00204     <span class="keywordflow">if</span> (!<a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>()) {
<a name="l00205"></a>00205       <a class="code" href="namespaceinfact.html#a746fb382cf3fd6cf656f40669d90372c" title="Reports an error encountered during parsing and/or construction of an object.">Error</a>(<span class="stringliteral">&quot;invoking StreamTokenizer::Next when HasNext returns false&quot;</span>);
<a name="l00206"></a>00206     }
<a name="l00207"></a>00207 
<a name="l00208"></a>00208     <span class="keywordtype">size_t</span> curr_token_idx = next_token_idx_;
<a name="l00209"></a>00209 
<a name="l00210"></a>00210     <span class="comment">// Try to get the next token of the stream if we&#39;re about to run out of</span>
<a name="l00211"></a>00211     <span class="comment">// tokens.</span>
<a name="l00212"></a>00212     <span class="keywordflow">if</span> (!eof_reached_ &amp;&amp; next_token_idx_ + 1 == token_.size()) {
<a name="l00213"></a>00213       <a class="code" href="structinfact_1_1_stream_tokenizer_1_1_token.html" title="Information about a token read from the underlying stream.">Token</a> next;
<a name="l00214"></a>00214       <span class="keywordflow">if</span> (GetNext(&amp;next)) {
<a name="l00215"></a>00215         token_.push_back(next);
<a name="l00216"></a>00216       }
<a name="l00217"></a>00217     }
<a name="l00218"></a>00218     <span class="comment">// Ensure that we only advance if we haven&#39;t already reached token_.size().</span>
<a name="l00219"></a>00219     <span class="keywordflow">if</span> (next_token_idx_ &lt; token_.size()) {
<a name="l00220"></a>00220       ++next_token_idx_;
<a name="l00221"></a>00221     }
<a name="l00222"></a>00222 
<a name="l00223"></a>00223     <span class="keywordflow">return</span> token_[curr_token_idx].tok;
<a name="l00224"></a>00224   }
<a name="l00225"></a>00225 
<a name="l00228"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ab67fe58983f7d9c2afb917bb70b4d8dd">00228</a>   <span class="keywordtype">void</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ab67fe58983f7d9c2afb917bb70b4d8dd" title="Rewinds this token stream to the beginning.">Rewind</a>() {
<a name="l00229"></a>00229     next_token_idx_ = 0;
<a name="l00230"></a>00230   }
<a name="l00231"></a>00231 
<a name="l00236"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#ac982651a8dad8dafcae18cd8c5f65b45">00236</a>   <span class="keywordtype">void</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac982651a8dad8dafcae18cd8c5f65b45" title="Rewinds this token stream by the specified number of tokens.">Rewind</a>(<span class="keywordtype">size_t</span> num_tokens) {
<a name="l00237"></a>00237     <span class="comment">// Cannot rewind more than the number of tokens read so far.</span>
<a name="l00238"></a>00238     <span class="keywordflow">if</span> (num_tokens &gt; next_token_idx_) {
<a name="l00239"></a>00239       num_tokens = next_token_idx_;
<a name="l00240"></a>00240     }
<a name="l00241"></a>00241     next_token_idx_ -= num_tokens;
<a name="l00242"></a>00242   }
<a name="l00243"></a>00243 
<a name="l00245"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a77e027a255cb0db73632e1ccd036808a">00245</a>   <span class="keywordtype">void</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a77e027a255cb0db73632e1ccd036808a" title="A synonym for Rewind(1).">Putback</a>() {
<a name="l00246"></a>00246     <a class="code" href="classinfact_1_1_stream_tokenizer.html#ab67fe58983f7d9c2afb917bb70b4d8dd" title="Rewinds this token stream to the beginning.">Rewind</a>(1);
<a name="l00247"></a>00247   }
<a name="l00248"></a>00248 
<a name="l00251"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#acbafcc5e7f6a0b3e4cd8166c4a53f4a8">00251</a>   <span class="keywordtype">size_t</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#acbafcc5e7f6a0b3e4cd8166c4a53f4a8" title="Returns the next token&rsquo;s start position, or the byte position of the underlying byte stream if there ...">PeekTokenStart</a>()<span class="keyword"> const </span>{
<a name="l00252"></a>00252     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>() ? token_[next_token_idx_].start : num_read_;
<a name="l00253"></a>00253   }
<a name="l00254"></a>00254 
<a name="l00257"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a50a622f50ef6aa800a9c7c15b96bb6cb">00257</a>   <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3" title="The set of types of tokens read by this stream tokenizer.">TokenType</a> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a50a622f50ef6aa800a9c7c15b96bb6cb" title="Returns the type of the next token, or EOF_TYPE if there is no next token.">PeekTokenType</a>()<span class="keyword"> const </span>{
<a name="l00258"></a>00258     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>() ? token_[next_token_idx_].type : <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac6e5b0b274a66a2a504ac4fd425c57b3ab746a48f2f16545e93a343b15ce7f137">EOF_TYPE</a>;
<a name="l00259"></a>00259   }
<a name="l00260"></a>00260 
<a name="l00264"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a70c11e357a41f13d3c8e916fab290145">00264</a>   <span class="keywordtype">size_t</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a70c11e357a41f13d3c8e916fab290145" title="Returns the line number of the first byte of the next token, or the current line number of the underl...">PeekTokenLineNumber</a>()<span class="keyword"> const </span>{
<a name="l00265"></a>00265     <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>() ? token_[next_token_idx_].line_number : line_number_;
<a name="l00266"></a>00266   }
<a name="l00267"></a>00267 
<a name="l00271"></a><a class="code" href="classinfact_1_1_stream_tokenizer.html#a61ec5de244a34de264e5b18e4809d753">00271</a>   <span class="keywordtype">string</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#a61ec5de244a34de264e5b18e4809d753" title="Returns the next token that would be returned by the Next method.">Peek</a>()<span class="keyword"> const </span>{ <span class="keywordflow">return</span> <a class="code" href="classinfact_1_1_stream_tokenizer.html#ac8de4792d7dadb993d3e96c54df2cbe6" title="Returns whether there is another token in the token stream.">HasNext</a>() ? token_[next_token_idx_].tok : <span class="stringliteral">&quot;&quot;</span>; }
<a name="l00272"></a>00272 
<a name="l00273"></a>00273  <span class="keyword">private</span>:
<a name="l00274"></a>00274   <span class="keywordtype">void</span> Init(<span class="keyword">const</span> <span class="keywordtype">char</span> *reserved_chars) {
<a name="l00275"></a>00275     num_reserved_chars_ = strlen(reserved_chars);
<a name="l00276"></a>00276     reserved_chars_ = <span class="keyword">new</span> <span class="keywordtype">char</span>[num_reserved_chars_ + 1];
<a name="l00277"></a>00277     strcpy(reserved_chars_, reserved_chars);
<a name="l00278"></a>00278     <span class="keywordtype">int</span> num_reserved_words = <span class="keyword">sizeof</span>(default_reserved_words)/<span class="keyword">sizeof</span>(<span class="keyword">const</span> <span class="keywordtype">char</span>*);
<a name="l00279"></a>00279     <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i &lt; num_reserved_words; ++i) {
<a name="l00280"></a>00280       reserved_words_.insert(<span class="keywordtype">string</span>(default_reserved_words[i]));
<a name="l00281"></a>00281     }
<a name="l00282"></a>00282     Token next;
<a name="l00283"></a>00283     <span class="keywordflow">if</span> (GetNext(&amp;next)) {
<a name="l00284"></a>00284       token_.push_back(next);
<a name="l00285"></a>00285     }
<a name="l00286"></a>00286   }
<a name="l00287"></a>00287 
<a name="l00288"></a>00288   <span class="keywordtype">void</span> ConsumeChar(<span class="keywordtype">char</span> c);
<a name="l00289"></a>00289 
<a name="l00290"></a>00290   <span class="keywordtype">bool</span> ReadChar(<span class="keywordtype">char</span> *c);
<a name="l00291"></a>00291 
<a name="l00299"></a>00299   <span class="keywordtype">bool</span> GetNext(Token *next);
<a name="l00300"></a>00300 
<a name="l00303"></a>00303   <span class="keywordtype">bool</span> ReservedChar(<span class="keywordtype">char</span> c)<span class="keyword"> const </span>{
<a name="l00304"></a>00304     <span class="keywordflow">for</span> (<span class="keywordtype">size_t</span> i = 0; i &lt; num_reserved_chars_; ++i) {
<a name="l00305"></a>00305       <span class="keywordflow">if</span> (c == reserved_chars_[i]) {
<a name="l00306"></a>00306         <span class="keywordflow">return</span> <span class="keyword">true</span>;
<a name="l00307"></a>00307       }
<a name="l00308"></a>00308     }
<a name="l00309"></a>00309     <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00310"></a>00310   }
<a name="l00311"></a>00311 
<a name="l00312"></a>00312   <span class="comment">// data members</span>
<a name="l00313"></a>00313 
<a name="l00314"></a>00314   <span class="comment">// The stream itself.</span>
<a name="l00317"></a>00317 <span class="comment"></span>  istringstream sstream_;
<a name="l00319"></a>00319   istream &amp;is_;
<a name="l00320"></a>00320 
<a name="l00321"></a>00321   <span class="comment">// Information about special tokens.</span>
<a name="l00322"></a>00322   <span class="keywordtype">char</span> *reserved_chars_;
<a name="l00323"></a>00323   <span class="keywordtype">size_t</span> num_reserved_chars_;
<a name="l00324"></a>00324   set&lt;string&gt; reserved_words_;
<a name="l00325"></a>00325 
<a name="l00326"></a>00326   <span class="comment">// Information about the current state of the underlying byte stream.</span>
<a name="l00327"></a>00327   <span class="keywordtype">size_t</span> num_read_;
<a name="l00328"></a>00328   <span class="keywordtype">size_t</span> line_number_;
<a name="l00329"></a>00329   <span class="keywordtype">bool</span> eof_reached_;
<a name="l00330"></a>00330   ostringstream oss_;
<a name="l00331"></a>00331 
<a name="l00332"></a>00332   <span class="comment">// The sequence of tokens read so far.</span>
<a name="l00333"></a>00333   vector&lt;Token&gt; token_;
<a name="l00334"></a>00334 
<a name="l00335"></a>00335   <span class="comment">// The index of the next token in this stream in token_, or token_.size()</span>
<a name="l00336"></a>00336   <span class="comment">// if there are no more tokens left in this stream.  Note that invocations</span>
<a name="l00337"></a>00337   <span class="comment">// of the Rewind and Putback methods alter this data member.</span>
<a name="l00338"></a>00338   <span class="keywordtype">size_t</span> next_token_idx_;
<a name="l00339"></a>00339 };
<a name="l00340"></a>00340 
<a name="l00341"></a>00341 }  <span class="comment">// namespace infact</span>
<a name="l00342"></a>00342 
<a name="l00343"></a>00343 <span class="preprocessor">#endif</span>
</pre></div></div><!-- contents -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Defines</a></div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>



<hr class="footer"/><address class="footer"><small>
Generated on Wed Apr 23 2014 10:07:52 for InFact by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/>
</a> 1.7.6.1
</small></address>

</body>
</html>
